/*
 * A command line application for AWS Glacier built on top of AWS Glacier Java SDK code samples (high level)
 * Allows to operate Vault and Archive entities: 
 * - create vault, delete vault, get vault description, get vaults list
 * - upload a file creating a new archive for that, download an archive, delete an archive
 * 
 * Distributed under Apache 2.0 license
 * Author: David Mail, email: david.mail37(at)gmail(dot)com
 * October 7 2012
 */
package com.pixamba.aws.glacier;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class AwsGlacierApp {
    /**
     * @param args the command line arguments
     */
    public static void main( String[] args) {
        if( args.length < 3){ // should be at least 3 params: config_file vault|archive command
            AwsGlacierApp.printUsage();
            System.exit( 1);
        }
        
        String configFile = args[0];
        int index = 2;
        try{
            if( args[1].equals("vault")){
                if( args.length < index + 1){
                    AwsGlacierApp.printUsage();
                    System.exit( 1);
                }
                
                AwsGlacierVault vault = new AwsGlacierVault( configFile);

                if( args[index].equals( "create") ||  args[index].equals( "describe") || args[index].equals( "delete")){
                    if( args.length < index + 2){
                        AwsGlacierApp.printUsage();
                        System.exit( 1);
                    }
                    String vaultName = args[index + 1];

                    if( args[index].equals( "create")){
                        // create the vault and print its location. Prints smth like '/1234567/vaults/test1' if the vault name was 'test1'
                        System.out.println( vault.create( vaultName));
                        System.exit( 0);
                    } else if( args[index].equals( "describe")){
                        // print vault description
                        System.out.println( vault.describe( vaultName));
                        System.exit( 0);
                    } else if( args[index].equals( "delete")){
                        // delete the vault
                        vault.delete( vaultName);
                        System.exit( 0);
                    }
                } else if( args[index].equals( "list")){
                    System.out.println( vault.list());
                    System.exit( 0);
                } else {
                        AwsGlacierApp.printUsage();
                        System.exit( 1);
                }
            } // End of vault case
            else if( args[1].equals("archive")){ // archive case
                AwsGlacierArchive archive = new AwsGlacierArchive( configFile);
                
                if( args[index].equals( "upload")){
                    if( args.length < index + 4){
                        AwsGlacierApp.printUsage();
                        System.exit( 1);
                    }
                    // upload the file creating a new archive and print the new archive ID
                    System.out.println( archive.upload( args[index + 1], args[index + 2], args[index + 3]));
                    System.exit( 0);
                } else if( args[index].equals( "download")){
                    if( args.length < index + 4){
                        AwsGlacierApp.printUsage();
                        System.exit( 1);
                    }
                    // download the archive to the specified file
                    // NOTE: Due to Glacier acrhitecture this call will take hours!! (at least 4 hours, but could be even more!)
                    archive.download( args[index + 1], args[index + 2], args[index + 3]);
                    System.exit( 0);
                } else if( args[index].equals( "delete")){
                    if( args.length < index + 3){
                        AwsGlacierApp.printUsage();
                        System.exit( 1);
                    }
                    // delete the archive
                    archive.delete( args[index + 1], args[index + 2]);
                    System.exit( 0);
                } else {
                    AwsGlacierApp.printUsage();
                    System.exit( 1);
                }
            } // End of archive case
            else { // neither 'vault' not the 'archive' command? not good...
                AwsGlacierApp.printUsage();
                System.exit( 1);
            }
        } catch( Exception e){
            System.err.print( e.getMessage());
            e.printStackTrace();
        }
        
    }
    
    public static void printUsage(){
        System.out.println( "Usage: java -jar AwsGlacierApp.jar <confg-file> <vault|archive> ...see below for params...");
        System.out.println( "vault create <name>");
        System.out.println( "vault describe <name>");
        System.out.println( "vault delete <name>");
        System.out.println( "vault list");
        System.out.println( "archive upload <vaultName> <newArchiveName> <filePathToBeArchived>");
        System.out.println( "archive download <vaultName> <archiveId> <newFilePathToStoreTheArchive>");
        System.out.println( "archive delete <vaultName> <archiveId>");
        System.out.println( "-------------------------------");
        System.out.println( "** NOTE ** Due to Glacier architecture downloading an archive takes many hours! The app will block until download is finished **");
    }
}
